
/*
'===============================================================================
'  HNG.Portal.BL.TblSupportOnlineManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace HNG.Portal.BLL
{
	public class TblSupportOnlineManager : TblSupportOnlineManagerBase
	{
        SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblSupportOnlineManager()
		{
			// Nothing for now.
		}
        public DataTable be_selectToGridView(TblSupportOnlineEntity _TblSupportOnlineEntity)
        {
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            SqlParameter[] _SqlParameter = new SqlParameter[3];
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblSupportOnlineEntity.SortExpression) ? "[ID]" : _TblSupportOnlineEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblSupportOnlineEntity.SortDirection) ? " DESC" : _TblSupportOnlineEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[Id],").Append("\n");
                sbSql.Append("  ch.[HoTen],").Append("\n");
                sbSql.Append("  ch.[Phone],").Append("\n");
                sbSql.Append("  ch.[Icon],").Append("\n");
                sbSql.Append("  ch.[Size],").Append("\n");
                sbSql.Append("  ch.[Email],").Append("\n");
                sbSql.Append("  ch.[ChucVu],").Append("\n");
                sbSql.Append("  ch.[NickSupport],").Append("\n");
                sbSql.Append("  ch.[TypeSupport],").Append("\n");
                sbSql.Append("  ch.[TrangThai]").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblSupportOnline ch").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblSupportOnlineEntity.ID);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");

                }
                string strCon = _TblSupportOnlineEntity.Condition;
                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.Condition))
                {
                    sbSql.AppendFormat("  AND (ch.[HoTen] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Phone] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TypeSupport] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[ChucVu] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[NickSupport] LIKE '%{0}%'", strCon).Append("\n");

                    sbSql.AppendFormat("  OR ch.[HoTen] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Phone] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TypeSupport] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[ChucVu] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[NickSupport] LIKE N'%{0}%'", strCon).Append("\n");

                    sbSql.Append(")").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.LangId))
                {
                    _SqlParameter[1] = new SqlParameter("@LangId", _TblSupportOnlineEntity.LangId);
                    sbSql.Append("  AND ch.[LangId] LIKE @LangId").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.Status))
                {
                    _SqlParameter[2] = new SqlParameter("@TrangThai", _TblSupportOnlineEntity.TrangThai);
                    sbSql.Append("  AND ch.[TrangThai] LIKE @TrangThai").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblSupportOnlineEntity.StartIndex, _TblSupportOnlineEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString(), _SqlParameter);
        }

	}
}
